package com.calvin.study.multithread.threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo01 {

	public static void main(String[] args) {
		//创建“固定数量的线程池”
		 fixedThread();

		// 创建只有一个线程的线程池
		//singleThread();
	}

	public static void fixedThread() {
		ExecutorService executor = Executors.newFixedThreadPool(3);// 线程池总量（银行网点办理业务窗口总数）
		try {
			// 办理业务的客户，10个人（开启10个线程）
			for (int i = 1; i <= 10; i++) {
				executor.submit(() -> {
					System.out.println("窗口" + Thread.currentThread().getName() + "\t 办理业务");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			executor.shutdown();
		}
	}

	public static void singleThread() {
		ExecutorService executor = Executors.newSingleThreadExecutor();
		try {
			for (int i = 1; i <= 10; i++) {
				executor.execute(() -> {
					System.out.println("[窗口" + Thread.currentThread().getName() + "] \t 办理业务");
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			executor.shutdown();
		}
	}

}
